package com.xishu.task;

import com.xishu.config.Config;
import com.xishu.config.Constant;
import com.xishu.entity.plat.CachePlatReport;
import com.xishu.entity.shop.Shop;
import com.xishu.report.CacheReportService;
import com.xishu.service.CommonService;
import com.xishu.service.LeaderService;
import com.xishu.util.DateUtil;
import com.xishu.util.JRedisUtil;
import com.xishu.util.MongoId;
import com.xishu.util.Tools;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ScanTask implements Tools {
    private static Logger logger = LoggerFactory.getLogger(ScanTask.class);
    private CacheReportService cacheReportService = CacheReportService.getInstance();

    //只在主节点执行
    @Scheduled(initialDelay = 0, fixedDelay = 30 * 1000)
    public void action() throws Exception {
        ThreadContext.put(Constant.REQUEST_ID, "scan task");

        Config config = Config.getInstance();
        if (!config.reportOpen()) {
            logger.info("report open false");
            return;
        }

        LeaderService leaderService = LeaderService.getInstance();
        if (!leaderService.elect()) {
            logger.info("elect failed");
            return;
        }

        CommonService commonService = CommonService.getInstance();
        long reportTime = System.currentTimeMillis();

        logger.info("master node  deal report");
        //清空30秒没有请求的B端以及C端的在线数
        JRedisUtil.zremrangeByScore(Constant.C_END_ONLINE_SORT_SET, 0, reportTime - DateUtil.ONE_SECONDS * 30);
        JRedisUtil.zremrangeByScore(Constant.B_END_ONLINE_SORT_SET, 0, reportTime - DateUtil.ONE_SECONDS * 30);

        CachePlatReport cachePlatReport = CacheReportService.getInstance().findCachePlatReport();
        if (reportTime - getLong(cachePlatReport.getRefreshTime()) < Config.getInstance().onlineCacheToDbTime()) {
            logger.info("not the online data  sync to db time");
        } else {
            //统计数据下盘
            logger.info("time to sync to db");
            long onlineNumberInB = JRedisUtil.zcard(Constant.B_END_ONLINE_SORT_SET);
            Long lastReportTime = getLong(cachePlatReport.getRefreshTime());

            cachePlatReport.setOnlineInB(onlineNumberInB);
            cachePlatReport.setRefreshTime(reportTime);
            cachePlatReport.setRefreshDate(DateUtil.formatDate(reportTime));
            commonService.save(cachePlatReport);

            //所有分店过去5分钟的订单情况
            List<Shop> shopList = CommonService.getInstance().searchAll(Shop.class);

            //5分钟单位时间内的订单统计
            long unitorderConfirmNumber = 0;
            long unitPayedOrderNumber = 0;

            //统计已落单的订单
            //统计支付订单
            for (Shop shop : shopList) {
                //5分钟内的订单数量，做累加
                long shopOrderNumber = CacheReportService.getInstance().countShopConfirmOrder(shop, reportTime, lastReportTime);
                long shopPayedOrderNumber = CacheReportService.getInstance().countShopPayedOrder(shop, reportTime, lastReportTime);

                unitorderConfirmNumber = unitorderConfirmNumber + shopOrderNumber;
                unitPayedOrderNumber = unitPayedOrderNumber + shopPayedOrderNumber;
            }

            //创建C端在线人数
            long onlineNumberInC = JRedisUtil.zcard(Constant.C_END_ONLINE_SORT_SET);
            cacheReportService.createOnlineInCReport(reportTime, onlineNumberInC);


            //最近5分钟内的订单数量
            cacheReportService.createUnitTimePlatShopOrder(unitorderConfirmNumber, reportTime);
            cacheReportService.createUnitTimePlatPayedOrder(unitPayedOrderNumber, reportTime);

            long totalOrderNumber = 0;
            long totalPayedNumber = 0;

            //统计支付订单
            for (Shop shop : shopList) {
                //totalOrderNumber = totalOrderNumber + cacheReportService.createShopOrder(shop, reportTime);
                //totalPayedNumber = totalPayedNumber + cacheReportService.createPayedShopOrder(shop, reportTime);
            }

            //创建平台级别的订单情况
            //cacheReportService.createPlatShopOrder(totalOrderNumber, reportTime);
            //cacheReportService.createPlatPayedOrder(totalPayedNumber, reportTime);
        }
    }
}

